package com.bwjf.dzfp.common.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class DateUtil {

	private static String DATETIME_FORMAT = "yyyy-MM-dd HH:mm";

	private static String DATE_FORMAT = "yyyy-MM-dd";
	private static String DATE_FORMAT_CHINESE = "yyyy年MM月dd日";

	private static String FULL_DATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss";

	private static String DATE_MONTH = "yyyyMM";

	/**
	 * Get the previous time, from how many days to now.
	 * 
	 * @param days
	 *            How many days.
	 * @return The new previous time.
	 */
	public static Date previous(int days) {
		return new Date(System.currentTimeMillis() - days * 3600000L * 24L);
	}

	/**
	 * 
	 * @return String yyyyMMddhhmmss
	 * 
	 */
	public static String formatDateForId(Date d) {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
				"yyyyMMddhhmmss");
		String time = simpleDateFormat.format(d);
		return time;
	}

	public static String formatDateYYYYMMDDHHMMSS(String date) {
		String time = date;
		if (date != null && date.indexOf("-") != -1) {
			Date d = DateUtil.parseDateByFomart(date, "yyyy-MM-dd hh:mm:ss");
			SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
					"yyyyMMddhhmmss");
			time = simpleDateFormat.format(d);
		}
		return time;
	}

	/**
	 * Convert date and time to string like "yyyy-MM-dd HH:mm".
	 */
	public static String formatDateTime(Date d) {
		return new SimpleDateFormat(DATETIME_FORMAT).format(d);
	}

	/**
	 * Convert date and time to string like "yyyy-MM-dd HH:mm".
	 */
	public static String formatDate(Date d, String format) {
		return new SimpleDateFormat(format).format(d);
	}

	/**
	 * Convert date and time to string like "hhmmss".
	 */
	public static String formatTime(Date d) {
		return new SimpleDateFormat("hhmmss").format(d);
	}

	/**
	 * Convert date and time to string like "yyyy-MM-dd HH:mm".
	 */
	public static String formatDateTime(long d) {
		return new SimpleDateFormat(DATETIME_FORMAT).format(d);
	}

	/**
	 * Convert date and time to string like "yyyy-MM-dd HH:mm:ss".
	 */
	public static String formatFullDateTime(Date d) {
		return new SimpleDateFormat(FULL_DATETIME_FORMAT).format(d);
	}

	/**
	 * Convert date to String like "yyyy-MM-dd".
	 */
	public static String formatDate(Date d) {
		return new SimpleDateFormat(DATE_FORMAT).format(d);
	}

	/**
	 * Convert date to String like "yyyyMMdd".
	 */
	public static String getDateTimeForCFT() {
		String pattern = "yyyyMMdd";
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		String dt = sdf.format(new Date());
		return dt;
	}

	/**
	 * Parse date like "yyyy-MM-dd".
	 */
	public static Date parseDate(String d) {
		try {
			return new SimpleDateFormat(DATE_FORMAT).parse(d);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * Parse date like
	 */
	public static Date parseDateByFomart(String d, String formart) {
		try {
			return new SimpleDateFormat(formart).parse(d);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * Parse date and time like "yyyy-MM-dd hh:mm".
	 */
	public static Date parseDateTime(String dt) {
		try {
			return new SimpleDateFormat(DATETIME_FORMAT).parse(dt);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * Parse date and time like "yyyy-MM-dd hh:mm:ss".
	 */
	public static Date parseFullDateTime(String dt) {
		try {
			return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(dt);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static Calendar switchStringToCalendar(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		return c;
	}

	/**
	 * Parse date and time like "hh:mm".
	 */
	public static Date parseHoursDateTime(String dt) {
		try {
			return new SimpleDateFormat("hh:mm").parse(dt);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * @param zhoubin
	 * @param date1
	 *            需要比较的时间 不能为空(null),需要正确的日期格式
	 * @param date2
	 *            被比较的时间 为空(null)则为当前时间
	 * @param stype
	 *            返回值类型 0为多少天，1为多少个月，2为多少年
	 * @return
	 */
	// public static int compareDate(String date1, String date2, int stype) {
	/*
	 * int n = 0;
	 * 
	 * String[] u = { "天", "月", "年" }; String formatStyle = stype == 1 ?
	 * "yyyy-MM" : "yyyy-MM-dd";
	 * 
	 * date2 = date2 == null ? DateTest.getCurrentDate() : date2;
	 * 
	 * DateFormat df = new SimpleDateFormat(formatStyle); Calendar c1 =
	 * Calendar.getInstance(); Calendar c2 = Calendar.getInstance(); try {
	 * c1.setTime(df.parse(date1)); c2.setTime(df.parse(date2)); } catch
	 * (Exception e3) { System.out.println("wrong occured"); } // List list =
	 * new ArrayList(); while (!c1.after(c2)) { // 循环对比，直到相等，n 就是所要的结果 //
	 * list.add(df.format(c1.getTime())); // 这里可以把间隔的日期存到数组中 打印出来 n++; if (stype
	 * == 1) { c1.add(Calendar.MONTH, 1); // 比较月份，月份+1 } else {
	 * c1.add(Calendar.DATE, 1); // 比较天数，日期+1 } }
	 * 
	 * n = n - 1;
	 * 
	 * if (stype == 2) { n = (int) n / 365; }
	 * 
	 * //System.out.println(date1 + " -- " + date2 + " 相差多少" + u[stype] + ":" +
	 * n); return n;
	 */
	// }

	/**
	 * 【时间】：12:05:42 PM 【说明】：格式化日期 【传参数】：@param date 【传参数】：@return
	 * 
	 * @return： String
	 */
	public static String getDateYYMMDD(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		return sdf.format(date);

	}

	/**
	 * 得到当前日期
	 * 
	 * @return
	 */
	public static String getCurrentDate() {
		Calendar c = Calendar.getInstance();
		Date date = c.getTime();
		SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd");
		return simple.format(date);

	}

	/**
	 * 得到当前月份
	 * 
	 * @return
	 */
	public static String getCurrentMonth() {
		Calendar c = Calendar.getInstance();
		Date date = c.getTime();
		SimpleDateFormat simple = new SimpleDateFormat(DATE_MONTH);
		return simple.format(date);

	}

	/**
	 * 获取某月的第一天
	 * 
	 * @throws
	 */
	public static String getFirstDayOfMonth(int year, int month) {
		Calendar cal = Calendar.getInstance();
		// 设置年份
		cal.set(Calendar.YEAR, year);
		// 设置月份
		cal.set(Calendar.MONTH, month - 1);
		// 获取某月最大天数
		int lastDay = cal.getActualMinimum(Calendar.DAY_OF_MONTH);
		// 设置日历中月份的最大天数
		cal.set(Calendar.DAY_OF_MONTH, lastDay);
		// 格式化日期
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String lastDayOfMonth = sdf.format(cal.getTime());

		return lastDayOfMonth;
	}

	/**
	 * 获取本年某月的第一天
	 * 
	 * @throws
	 */
	public static String getYearFirstDayOfMonth(int month) {
		int year = Calendar.getInstance().get(Calendar.YEAR);
		return getFirstDayOfMonth(year, month);
	}

	/**
	 * 获取本月的第一天
	 * 
	 * @throws
	 */
	public static String getNowFirstDayOfMonth() {
		int month = Calendar.getInstance().get(Calendar.MONTH) + 1;
		return getYearFirstDayOfMonth(month);
	}

	/**
	 * 获取某月的最后一天
	 * 
	 * @throws
	 */
	public static String getLastDayOfMonth(int year, int month) {
		Calendar cal = Calendar.getInstance();
		// 设置年份
		cal.set(Calendar.YEAR, year);
		// 设置月份
		cal.set(Calendar.MONTH, month - 1);
		// 获取某月最大天数
		int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
		// 设置日历中月份的最大天数
		cal.set(Calendar.DAY_OF_MONTH, lastDay);
		// 格式化日期
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String lastDayOfMonth = sdf.format(cal.getTime());

		return lastDayOfMonth;
	}

	/**
	 * 获取本年某月的最后一天
	 * 
	 * @throws
	 */
	public static String getYearLastDayOfMonth(int month) {
		int year = Calendar.getInstance().get(Calendar.YEAR);
		return getLastDayOfMonth(year, month);
	}

	/**
	 * 获取本月的最后一天
	 * 
	 * @throws
	 */
	public static String getNowLastDayOfMonth() {
		int month = Calendar.getInstance().get(Calendar.MONTH) + 1;
		return getYearLastDayOfMonth(month);
	}

	/**
	 * 获取当前月份
	 * 
	 * @throws
	 */
	public static int getThisMonth() {
		int month = Calendar.getInstance().get(Calendar.MONTH) + 1;
		return month;
	}

	/**
	 * 得到当前月份
	 * 
	 * @return
	 */
	public static Integer getMonth(Date date) {
		// Calendar c=c.g
		if (date == null) {
			return 0;
		} else {
			SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd");
			String str = simple.format(date);
			String[] moths = str.split("-");
			return (Integer.parseInt(moths[0]) - 1) * 12
					+ Integer.parseInt(moths[1]);
		}
	}

	/**
	 * 【时间】：12:05:42 PM 【说明】：格式化日期 【传参数】：@param date 【传参数】：@return
	 * 
	 * @return： String
	 */
	public static String getDateYYMMDD(Date date, String format) {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.format(date);

	}

	public static String getDateCHINESE(Date date) {
		if (date != null) {
			SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_CHINESE);
			return sdf.format(date);
		} else {
			return null;
		}
	}

	public static long fromDateStringToLong(String inVal) {
		Date date = null;
		SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-mm-dd hh:ss");
		try {
			date = inputFormat.parse(inVal);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return date.getTime();
	}

	public static Date stringToDate(String dte) {
		SimpleDateFormat sdf = new SimpleDateFormat(FULL_DATETIME_FORMAT);
		Date date = null;
		try {
			date = sdf.parse(dte);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return date;
	}

	/**
	 * 获取两个日期的间隔天数 计算格式sj1-sj2
	 * 
	 * @param sj1
	 * @param sj2
	 * @return
	 */
	public static int getTwoDay(String sj1, String sj2, String format) {
		SimpleDateFormat myFormatter = new SimpleDateFormat(format);
		long day = 0;
		try {
			Date date = myFormatter.parse(sj1);
			Date mydate = myFormatter.parse(sj2);
			day = (date.getTime() - mydate.getTime()) / (24 * 60 * 60 * 1000);
		} catch (Exception e) {
			return 0;
		}
		return (int) day;
	}

	public static void main(String[] args) {
		// //System.out.println(Integer.parseInt("01"));
		//
		// Calendar c=Calendar.getInstance();
		// Integer i1=new Integer(1);
		// Integer i2=new Integer(1);
		// c.setTime(new Date());
		// // System.out.println(DateUtil.getStringDateFormat(c.getTime(),
		// "yyyy-MM-dd "));
		// c.add(c.MONTH, -1);
		// System.out.println((i2-i1)==0);
		//
	}

	public static String YYYY_MM_DD() {
		return getCurrentTime("yyyy-MM-dd");
	}

	public static String getCurrentTime(String format) {
		GregorianCalendar rightNow = new GregorianCalendar();
		SimpleDateFormat formatter = new SimpleDateFormat(format);
		return formatter.format(rightNow.getTime());
	}

	/**
	 * Parse date and time like "yyyy-MM-dd hh:mm".
	 */
	public static Date parseDateTime(String dt, String format) {
		try {
			return new SimpleDateFormat(format).parse(dt);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static String toChese(String date) {
		Date d = null;
		try {
			d = new SimpleDateFormat(FULL_DATETIME_FORMAT).parse(date);
		} catch (Exception e) {
			d = null;
		}
		if (d == null) {
			try {
				d = new SimpleDateFormat("yyyyMMddhhmmss").parse(date);
			} catch (Exception e) {
				e.printStackTrace();
				d = null;
			}
		}
		if (d != null) {
			return getDateCHINESE(d);
		} else {
			return null;
		}

	}
}
